package com.bst.bsj.base.controller;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSON;

public class HttpResponse {

	private Map<String, String> map;

	public HttpResponse(int code) {
		this.map = new HashMap<String, String>();
		map.put("resultCode", Integer.toString(code));
	}

	public HttpResponse(StatusCode statusCode) {
		this(statusCode.value);
	}

	public HttpResponse(int statusCode, Serializable object) {
		this.map = new HashMap<String, String>();
		map.put("code", statusCode + "");
		if (object instanceof String) {
			map.put("data", (String) object);
		} else {
			if (null != object) {
				String objectStr = JSON.toJSONString(object);
				@SuppressWarnings("unchecked")
				Map<String, String> objectMap = JSON.parseObject(objectStr,
						Map.class);
				map.putAll(objectMap);
			}
		}
	}

	public String toJson() {
		return JSON.toJSONString(this.map);
	}

	public Map<String, String> getMap() {
		return map;
	}

	public void setMap(Map<String, String> map) {
		this.map = map;
	}

	public enum StatusCode {
		OK(200),

		INTERERR(500);

		private int value;

		private StatusCode(int value) {
			this.value = value;
		}

		public int getValue() {
			return this.value;
		}
	}
}
